package cn.nc.controller;


import cn.nc.entity.Article;
import cn.nc.entity.User;
import cn.nc.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/back")
public class ArticleController {

    @Autowired
    ArticleService articleService;



    //文章详情页
    @GetMapping("/article")
    public String articleManager(Model model){
        List<Article> articleList = articleService.findAll();
        model.addAttribute("articles",articleList);
        return "/back/article_manager";
    }

    //文章删除
    @GetMapping("article/delete/{id}")
    public String deleteArticle(@PathVariable("id")Integer id){
        articleService.deleteByArticleId(id);
        return "redirect:/page/user/1";
    }


    //修改文章界面
    @GetMapping("/article/edit/{id}")
    public String editAttribute(@PathVariable("id")Integer id,Model model){
        System.out.println(articleService.findArticleById(id));
        model.addAttribute("article",articleService.findArticleById(id));
        return "/back/article_edit";

    }


    //新增文章界面
    @GetMapping("/article/add")
    public  String addArticlePage(){
        return "back/article_edit";
    }



    //修改文章发布
    @PostMapping("/article/publish/{id}")
    public String editArticle(@PathVariable("id") Integer id, String title, String content){
        articleService.updateArticleById(id,title,content);
        return "redirect:/article/"+id;
    }

    //新增文章
    @PostMapping("/article/publish")
    public String addArticle(String title, String content){
        Article article = articleService.addArticle(title,content);
        return "redirect:/article/"+article.getId();
    }

}
